﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint.Client;

namespace CloudAppCsomDemoWeb.Pages {

  public class appWebTitle : IHttpHandler {

    public void ProcessRequest(HttpContext httpContext) {
      
      // retrieve app web url from cache
      string appWebUrl = httpContext.Cache["SPAppWebUrl"].ToString();
      
      string appWebTitle = string.Empty;
      using (ClientContext clientContext = new ClientContext(appWebUrl)) {
        Web appWeb = clientContext.Web;
        clientContext.Load(appWeb);
        clientContext.ExecuteQuery();
        appWebTitle = appWeb.Title;
      }

      // return response as simple text
      httpContext.Response.ContentType = "text/plain";
      httpContext.Response.Write(appWebTitle);
    }

    public bool IsReusable {
      get {return false;}
    }
  }
}